package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.*;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;
import com.beerandjesus.sportofkings.handicapper.strategy.RacingCalculator;
import com.beerandjesus.sportofkings.handicapper.factory.ResultsTableFactory;
import com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile.ResultsTableFactoryImpl;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public abstract class AbstractRaceAnalysisStrategy {

	protected static final Float ZERO = 0f;
	protected static final Float ELEVEN = new Float(11.0);
    protected static final String CLASS_ALL = "CLASS ALL";
	protected static final String CLASS_L2 = "CLASS L2";
	protected static final String AVG_PURSE_L2 = "AVG PURSE L2";

    protected RacingCalculator calc = new RacingCalculator();
	protected FastWorkoutResolver workResolver = new FastWorkoutResolver();

    private ResultsTableFactory resultsTableFactory = new ResultsTableFactoryImpl();

    public AbstractRaceAnalysisStrategy() {
		super();
	}
	
	protected Integer getInteger(String value, Integer theDefault) {
		try {
			return new Integer(value);
		} catch (Exception e) {
			return theDefault;
		}
		
	}

    protected void setCommonFields(RaceAnalysisResultVOImpl vo, Race r, List<Horse> horses) {
		vo.setFieldCount(horses.size());
		vo.setPurse(r.getPurse());
		vo.setClaimingPrice(r.getClaimingPrice());
		vo.setRaceName(r.getRaceClassification());
		vo.setRaceType(r.getType());
		vo.setSpeedPar(r.getBrisSpeedPar());
		vo.setSurface(r.getSurface());
		vo.setTrackCode(r.getTrack());
		vo.setRaceDate(r.getDate());
		vo.setRaceNumber(r.getRaceNumber());
		vo.setPostTimes(r.getPostTimes());
		vo.setDistance(calc.distanceFromYards(r.getDistance()));
	}

	public Float getFloatOrZero(String value) {
		try {
			return new Float(value.trim());
		} catch (Exception e) {
			return ZERO;
		}
	}

    protected boolean equal(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (i == new Integer(value));
        } catch (NumberFormatException e) {
            return false;
        }
    }

    protected boolean greaterThan(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (new Integer(value) > i);
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public ResultsTableFactory getResultsTableFactory() {
        return resultsTableFactory;
    }
}